package com.example.administrator.waitan.find.life.LifeContentActivity;

import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.os.IBinder;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.util.LruCache;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.example.administrator.waitan.R;
import com.example.administrator.waitan.tools.VolleySingleton;

import java.util.List;

/**
 * Created by Administrator on 2016/1/19.
 */
public class LifeContentViewPagerAdapter extends PagerAdapter {
    private Context context;
    private List<LifeContentInfo> data;
    private ImageLoader imageLoader;
    private ImageLoader.ImageCache imageCache;
    private LruCache<String, Bitmap> lruCache;
    private RequestQueue queue;
    private DownLoadContentService.DownLoadContentBinder downLoadContentBinder;
    public ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            downLoadContentBinder = (DownLoadContentService.DownLoadContentBinder) iBinder;
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            downLoadContentBinder = null;
        }

    };

    public LifeContentViewPagerAdapter(Context context, List<LifeContentInfo> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.viewpager_life_content, null);
        final ImageView pic = (ImageView) view.findViewById(R.id.iv_vp_life_content);
        VolleySingleton.getImageLoader().get(data.get(position).getMiddle(), VolleySingleton.getImageListener(pic));
        pic.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                View dialog = LayoutInflater.from(context).inflate(R.layout.dialog_download,null);
                builder.setView(dialog);
                builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        downLoadContentBinder.DownLoadContent(data.get(position).getMiddle(), pic);

                    }
                });
               builder.show();

                Toast.makeText(context, "haha", Toast.LENGTH_SHORT).show();
                return true;
            }
        });
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
    }
}
